﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Eoffice.Models;

namespace Eoffice.Controllers
{
    public class CvDiController : Controller
    {
        private EofficeDb db = new EofficeDb();

        //
        // GET: /CvDi/

        public ActionResult Index()
        {
            //var cvdis = db.CVDIs.Include(c => c.LOAICV).Include(c => c.NV).Include(c => c.LINHVUC);
            //return View(cvdis.ToList());
            string page = "1"; //so phan trang hien tai
            var pagesize = 10; //so ban ghi tren 1 trang
            var numOfNews = 0; //tong so ban ghi co duoc truoc khi phan trang
            int curpage = 0; // trang hien tai dung cho phan trang
            if (Request["page"] != null)
            {
                page = Request["page"];
                curpage = Convert.ToInt32(page) - 1;
            }
            var all = db.CVDIs.ToList();
            var pages = all.Skip(curpage * pagesize).Take(pagesize).ToList();

            //var pages = data.sp_Brands_Phantrang(page, productize, "", "").ToList();
            var url = Request.Path;
            numOfNews = all.Count;

            if (numOfNews > 0)
            {
                ViewBag.Pager = Phantrang.PhanTrang(pagesize, curpage, numOfNews, url);
            }
            else
            {
                ViewBag.Pager = "";
            }
            return View(pages);
        }

        //
        // GET: /CvDi/Details/5

        public ActionResult Details(string id = null)
        {
            CVDI cvdi = db.CVDIs.First(cv=>cv.SOCVDI==id);
            if (cvdi == null)
            {
                return HttpNotFound();
            }
            return View(cvdi);
        }

        //
        // GET: /CvDi/Create

        public ActionResult Create()
        {
            ViewBag.MALOAI = new SelectList(db.LOAICVs, "MALOAI", "TENLOAI");
            ViewBag.MSNV = new SelectList(db.NVs, "MSNV", "HOTENNV");
            ViewBag.MALV = new SelectList(db.LINHVUCs, "MALV", "TENLV");
            return View();
        }

        //
        // POST: /CvDi/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(CVDI cvdi, FormCollection form)
        {
            cvdi.SOCVDI = form["SOCVDI"];
            cvdi.NGAYCVDI = form["NGAYCVDI"];
            cvdi.GHICHUCVDI = form["GHICHUCVDI"];
            cvdi.TRICHYEUCVDI = form["TRICHYEUCVDI"];
            cvdi.MALOAI = form["MALOAI"];
            cvdi.MSNV = form["MSNV"];
            cvdi.MALV = form["MALV"];

            db.CVDIs.Add(cvdi);
            db.SaveChanges();

            ViewBag.MALOAI = new SelectList(db.LOAICVs, "MALOAI", "TENLOAI", cvdi.MALOAI);
            ViewBag.MSNV = new SelectList(db.NVs, "MSNV", "HOTENNV", cvdi.MSNV);
            ViewBag.MALV = new SelectList(db.LINHVUCs, "MALV", "TENLV", cvdi.MALV);

            return RedirectToAction("Index");
        }

        //
        // GET: /CvDi/Edit/5

        public ActionResult Edit(string id = null)
        {
            CVDI cvdi = db.CVDIs.Find(id);
            if (cvdi == null)
            {
                return HttpNotFound();
            }
            ViewBag.MALOAI = new SelectList(db.LOAICVs, "MALOAI", "TENLOAI", cvdi.MALOAI);
            ViewBag.MSNV = new SelectList(db.NVs, "MSNV", "HOTENNV", cvdi.MSNV);
            ViewBag.MALV = new SelectList(db.LINHVUCs, "MALV", "TENLV", cvdi.MALV);

            return View(cvdi);
        }

        //
        // POST: /CvDi/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(string id, FormCollection form)
        {
            CVDI cvdi = db.CVDIs.First(cvd => cvd.SOCVDI == id);

            //cvdi.SOCVDI = form["SOCVDI"];
            cvdi.NGAYCVDI = form["NGAYCVDI"];
            cvdi.GHICHUCVDI = form["GHICHUCVDI"];
            cvdi.TRICHYEUCVDI = form["TRICHYEUCVDI"];
            cvdi.MALOAI = form["MALOAI"];
            cvdi.MSNV = form["MSNV"];
            cvdi.MALV = form["MALV"];
            cvdi.NGUOINHAN = form["NGUOINHAN"];

            db.SaveChanges();
            
            ViewBag.MALOAI = new SelectList(db.LOAICVs, "MALOAI", "TENLOAI", cvdi.MALOAI);
            ViewBag.MSNV = new SelectList(db.NVs, "MSNV", "HOTENNV", cvdi.MSNV);
            ViewBag.MALV = new SelectList(db.LINHVUCs, "MALV", "TENLV", cvdi.MALV);

            return RedirectToAction("Index");
        }

        //
        // GET: /CvDi/Delete/5

        public ActionResult Delete(string id = null)
        {
            CVDI cvdi = db.CVDIs.Find(id);
            if (cvdi == null)
            {
                return HttpNotFound();
            }
            return View(cvdi);
        }

        //
        // POST: /CvDi/Delete/5

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(string id)
        {
            CVDI cvdi = db.CVDIs.Find(id);
            db.CVDIs.Remove(cvdi);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}